<template>
	<s-page custom-class="Popup-Page">
		<!-- 	<view>
		<image src="/static/image/test/run_test.gif" mode="aspectFill" class="run run_test"></image>
		<button type="primary"  @click="login()">登录</button>
	</view> -->
		<view class="uni-common-mt">
			<view class="logo-box">
				<image src="/static/image/bwlogo.png" mode="aspectFit" class="login-logo"></image>
			</view>
			<form class="loginForm" @submit="bindlogin">
				<view class="uni-form-item uni-column" style="width: 690rpx;">
					<!-- <input class="my_input" id="my_input" name="input" v-model="username" placeholder="请输入账户名" /> -->
					<uni-easyinput class="my_input" id="my_input" name="input" v-model="username" placeholder="请输入账户名"
						:styles="styles" trim="all" :clearable="true">
					</uni-easyinput>
				</view>
				<view class="uni-form-item uni-column" style="margin-bottom: 70px; width: 690rpx;">
					<!-- <input class="my_input" id="my_input" password name="input" v-model="password" placeholder="请输入密码" /> -->
					<uni-easyinput class="my_input" id="my_input" type="password" name="input" v-model="password"
						placeholder="请输入密码" :styles="styles" trim="all">
					</uni-easyinput>
				</view>
				<!-- <view class="forget-register">
				<text class="forget">忘记密码</text>
				<text>|</text>
				<text class="register">新用户注册</text>
			</view> -->
				<view class="uni-btn-v">
					<button form-type="submit" type="primary">登录</button>
				</view>
				<!-- <uni-icons type="cart" size="30"></uni-icons> -->
			</form>


		</view>

		<s-popup v-model="autoCloseVisible" animate="zoom-in" position="center" background="#fff" width="400"
			:duration="2000" radius="20">
			<view class="s-popup-view">
				<text class="s-popup-title">{{s_title}}</text>
				<text class="s-popup-main">{{s_errorMsg}}</text>
			</view>
		</s-popup>
	</s-page>
</template>

<script>
	// import {
	// 	mapMutations
	// } from 'vuex';
	import Dialog from '@/uni_modules/s-ui/utils/dialog'; //弹出框引入
	export default {

		data() {
			return {
				// username: 'admin',
				// password: '1qaz2wsx'
				username: '',
				password: '',
				styles: {
					borderColor: '#EBEFF6'
				},
				groupHouseNo:'',
				id:'',
				autoCloseVisible: false, //弹出框
				s_errorMsg: '', //报错提示
				s_title: '',
			}
		},
		onLoad() {

			let that = this;

			// console.log(uni.getStorageSync('HC_name'))
			// console.log("SB")
			//缓存的账号
			const HC_name = uni.getStorageSync('HC_name');
			//缓存的密码
			const HC_pass = uni.getStorageSync('HC_pass');

			//有缓存就赋值给文本没有就清空
			if (HC_name && HC_pass) {
				that.username = HC_name;
				that.password = HC_pass;
			} else {
				that.username = '';
				that.password = '';
			}

			// if(uni.getStorageSync('loginAndReturnPage')!=''){
			// 	// console.log("true")
			// 	uni.switchTab({
			// 		url: '/pages/index/index'
			// 	});
			// }else{
			// 	// console.log("false")
			// }

		},
		methods: {
			onNavigationBarButtonTap() {
				// console.log("123")
				uni.switchTab({
					url: '/pages/index/index'
				});
			},
			async bindlogin() { //async 定义的函数会默认的返回一个Promise对象resolve的值
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-portal/login',
						method: "post",
						data: {
							userName: this.username,
							password: this.password,
							moduleName: "workerApp"
						},
					})
				console.log(res)
				let errorMsg = res.msg
				if (res.success == true) { //如果登录成功
				this.id=res.body.currentUser.id
				this.groupStaff()
					this.getAppConfig()
					// console.log(JSON.stringify(res))
					uni.setStorageSync('loginName', res.body.currentUser.loginName)
					uni.setStorageSync('name', res.body.currentUser.name)
					uni.setStorageSync('id', res.body.currentUser.id)
					uni.setStorageSync('deptid', res.body.currentUser.department.id)
					// 缓存账号密码
					uni.setStorageSync('HC_name', this.username);
					uni.setStorageSync('HC_pass', this.password);
					// 储存账户信息
					// uni.setStorageSync("systemRole",res.body.currentUser.systemRole)

					// console.log("res======================")
					// this.login(res.body.currentUser);
					// console.log("res++++++++++++++")

					// 缓存系统角色登记
					uni.setStorageSync('systemRole', res.body.currentUser.systemRole)
					let modular = res.body.menu.children[0].children
					let worker = res.body.menu.children[0].nodeName
					uni.setStorageSync('worker', worker)
					// console.log("worker",worker)
					let nodekeyArr = []
					for (let i = 0; i < modular.length; i++) {
						let nodeKey = modular[i].nodeKey
						let nodeName = modular[i].nodeName
						let url = modular[i].data.url
						let imgUrl = modular[i].data.imgUrl
						nodekeyArr.push({
							nodeKey: nodeKey,
							nodeName: nodeName,
							url: url,
							imgUrl: imgUrl
						})
					}
					console.log("nodekeyArr", nodekeyArr)
					uni.setStorageSync('nodekeyArr', nodekeyArr)

					// console.log("modular=", modular)
					var loginName = uni.getStorageSync('loginName')
					uni.setStorageSync('x-auth-token', res.body.currentUser.idToken); //获取服务端传递过来的token信息
					//下述代码用于模拟：当登录后台用户时，自动登录用户
					// uni.showLoading({
					// 	title: '正在登录涂鸦智能平台'
					// });
					try {
						const userModule = uni.requireNativePlugin("LJY-UserModule") //调用android中自定义的插件名称
						//执行涂鸦智能登录,使用账户密码登录
						const ret = userModule.login({ //login表示自定义插件的函数名，第一个表示要传递的参数
							userName: "13123481660", //18077799766,ay1627002092336IE868
							password: "12345678" //1qaz2wsx34
						});
						//使用uid登录
						/* const ret=userModule.loginOrRegisterWithUid({//login表示自定义插件的函数名，第一个表示要传递的参数
							uid: "ay1626790038401woBYc",//18077799766,ay1627002092336IE868
							password: "123456"//1qaz2wsx34
						}); */
						uni.hideLoading(); //加载结束
						console.log(ret);
						//弹出涂鸦智能登录结果
						// uni.showModal({
						// 	title: '系统提示++',
						// 	content: ret.msg,
						// 	showCancel: false,
						// 	success: function(res) {
						// 		if (res.confirm) {
						this.s_errorMsg = '2秒后自动跳转至首页'
						this.s_title = '登录成功'
						this.autoCloseVisible = true;
						setTimeout(function() {
							uni.switchTab({
								url: '/pages/index/index'
							});
						}, 2000);

						// 		}
						// 	}
						// });
					} catch (e) { //如果涂鸦智能无法连接
						uni.hideLoading(); //加载结束
						// uni.showModal({
						// 	title: '系统提示',
						// 	content: "涂鸦智能家居暂无法登录，将无法使用涂鸦智能家居功能",
						// 	showCancel: false,
						// 	success: function(res) {
						// 		if (res.confirm) {

						this.s_errorMsg = '2秒后自动跳转至首页'
						this.s_title = '登录成功'
						this.autoCloseVisible = true;
						setTimeout(function() {
							uni.switchTab({
								url: '/pages/index/index'
							});
						}, 2000);
						// 		}
						// 	}
						// });
					}
				} else {
					// uni.setStorageSync('HC_name', '');
					// uni.setStorageSync('HC_pass', '');
					// uni.showToast({
					// 	title: errorMsg,
					// 	icon:'error',
					// 	mask: true,
					// 	duration: 2000
					// });
					this.s_errorMsg = errorMsg
					this.s_title = '登录失败'
					this.autoCloseVisible = true
				}
			},
			async getAppConfig() {
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-mobile/config/1',
						method: "get",
					})
				// console.log(res)
				uni.setStorageSync('AppConfig', res.body.command)
			},
			//谭宇加获取员工负责的楼层
			async groupStaff(){
				const res = await this.$myRequest({
					url: '/api-operate/group/staffId/'+this.id,
					method: "get",
				})
				if (res.success == true) {
					console.log("this.groupStaffs")
					this.groupStaffs=res.body.groupStaff
					console.log(this.groupStaffs)
					if(this.groupStaffs.length!=0){
						var groupHouseNos=null
							for (let i = 0; i < this.groupStaffs.length; i++) {
								
								if(groupHouseNos==null){
									groupHouseNos = this.groupStaffs[i].houseNo;
								}else{
									groupHouseNos =groupHouseNos+','+ this.groupStaffs[i].houseNo;
								}
							}
							this.groupHouseNo=groupHouseNos
							uni.setStorageSync('groupHouseNo', this.groupHouseNo)
							console.log(this.groupHouseNo);
					}
				}
			},
			/* 			testAndroid(){
							const loginModule=uni.requireNativePlugin("ljy-testModule")//调用android中自定义的插件名称
							const ret=loginModule.login({//login表示自定义插件的函数名，第一个表示要传递的参数
								userName: this.username,
								password: this.password
							});
							uni.showModal({
								title: '系统提示',
								content:ret.msg,
								showCancel:false
							});
						} */
			// ...mapMutations(['login'])
		}
	}
</script>

<style lang="scss">
	.logo-box {
		width: 100%;
		height: 328rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.login-logo {
		// width: 200rpx;
		height: 200rpx;
		// background-color: #109EF8;
		// border-radius: 50%;
	}

	/* @import url("/static/style/test.css"); */
	.run {
		width: 750rpx; //750rpx,350rpx
	}


	form.loginForm>view {

		// margin: 50rpx;
	}

	.my_input {

		height: 76rpx;
		margin: 20rpx 10rpx;
		// margin: 0 36rpx 10rpx 36rpx;
		line-height: 76rpx;
		position: relative;
	}

	#my_input::after {

		content: "";
		width: 100%;
		height: 2px;
		background: #DBDBDB;
		position: absolute;
		bottom: 0;
		left: -20rpx;
		right: 0;
		margin: 0 auto;
	}

	.forget-register {
		color: #B0B0B0;
		margin: 62rpx 0 84rpx 0;
		padding: 0 36rpx 0 0;
		display: flex;
		justify-content: flex-end;
	}

	.forget-register text:nth-child(2) {
		margin: 0 20rpx 0 20rpx;
	}

	.uni-btn-v {
		margin: 0 auto;
		width: 700rpx;
	}

	.uni-form-item {
		// border: 1px solid #FF8095;
		margin: 0 auto;
	}

	.s-popup-view {
		// background-color: #0062CC;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.s-popup-title {
			// display: block;
			// position: absolute;
			// top: 30rpx;
			font-size: 38rpx;
			height: 76rpx;
			line-height: 76rpx;
		}

		.s-popup-main {
			font-size: 34rpx;
			margin: 30rpx 30rpx;
			// position: absolute;
			// bottom: 0;
		}

	}
</style>
